package com.weixin1.servlet;

import com.weixin1.bean.Photo;
import com.weixin1.dto.R;
import com.weixin1.service.PhotoService;
import com.weixin1.utils.JSONUtil;
import com.weixin1.utils.MyBeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@WebServlet("/photoservlet/*")
@MultipartConfig
public class PhotoServlet extends HttpServlet {
    PhotoService ps=new PhotoService();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String pathInfo = req.getPathInfo().substring(1);
        if("list".equals(pathInfo)){
            list(req,resp);
        }else if("upload".equals(pathInfo)){
            upload(req,resp);
        }else if("save".equals(pathInfo)){
            save(req,resp);
        }else if ("removeById".equals(pathInfo)){
            removeById(req,resp);
        }
    }

    private void removeById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Long pId = Long.valueOf(req.getParameter("pId"));
        String pPath = req.getParameter("pPath");
        try {
            File file=new File(pPath);
            file.delete();
            boolean b = ps.removeById(pId);
            if (b){
                resp.getWriter().print(JSONUtil.toString(R.ok()));
            }else {
                resp.getWriter().print(JSONUtil.toString(R.error()));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//        String pName = req.getParameter("pName");
//        String pPath = req.getParameter("pPath");
//        String eName = req.getParameter("eName");
//        Photo photo=new Photo();
//        photo.setPName(pName);
//        photo.setPPath(pPath);
//        photo.setEName(eName);
        Photo photo = MyBeanUtils.reqToBean(req, Photo.class);
        try {
            boolean b = ps.save(photo);
            if (b){
                resp.getWriter().print(JSONUtil.toString(R.ok()));
            }else {
                resp.getWriter().print(JSONUtil.toString(R.error()));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void upload(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        Part part = req.getPart("file");
        String fileName = part.getSubmittedFileName();


        //得到文件扩展名
        String ext = fileName.substring(fileName.lastIndexOf("."));

        //得到type和id
//        String type = req.getParameter("type");
//        String id = req.getParameter("id");
//
//        String realPath = null;

//        if(type!=null&&!type.equals("")){
//            //得到硬盘路径：
//            realPath = getServletContext().getRealPath("/upload/"+type+"/"+id+"/");
//        }else{
//            //得到硬盘路径：
//            realPath = getServletContext().getRealPath("C:/Users/Administrator/Desktop/javawebproject/src/main/webapp/admin/upload");
//        }

        //创建文件夹
//        new File(realPath).mkdirs();
        //把上传文件写入硬盘:
        String path="F:/javawebproject/src/main/webapp/admin/upload/" + UUID.randomUUID() + ext;

        part.write(path);

        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("path",path);
        resp.getWriter().print(JSONUtil.toString(map));
    }

    //查询所有照片
    private void list(HttpServletRequest req, HttpServletResponse resp) {
        Integer page = Integer.valueOf(req.getParameter("page"));
        Integer limit = Integer.valueOf(req.getParameter("limit"));
        try {
            List<Photo> list = ps.selectBypage(page,limit);
            Map<String,Object> map=new HashMap<>();
            int total=ps.gettotal();

            map.put("code",0);
            map.put("data",list);
            map.put("count",total);
            resp.getWriter().print(JSONUtil.toString(map));
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
